// 1. 导入mongoose
const mongoose = require('mongoose');
// 2. 连接数据库
mongoose.connect('mongodb://127.0.0.1/0907');
// 3. 创建连接回调 connection.on("open",callback)
mongoose.connection.on('open',()=>{
    console.log("连接成功");
    // 5. 创建文档结构 new mongoose.Schema()
    const userSchema = new mongoose.Schema({
        name:String,
        age:Number,
        score:Number
    });
    // 6. 创建模型对象 mongoose.model()
    const userModel = mongoose.model('users',userSchema);
    // 7. 单条更新 model.updateOne({条件},{更新值},callback)
    // 参数1：查询条件
    // 参数2：更新的值
    // 参数3：回调函数
    // userModel.updateOne({name:"黄政"},{score:101},(err,data)=>{
    //     if(err) throw err;
    //     console.log(data);
    //     /**
    //      * {
    //         acknowledged: true,
    //         modifiedCount: 1,
    //         upsertedId: null,
    //         upsertedCount: 0,
    //         matchedCount: 1
    //         }
    //      */
    // })

    // 8. 批量更新 model.updateMany({条件},{更新值}.callback)
    userModel.updateMany({score:100},{age:66},(err,data)=>{
        if(err) throw err;

        console.log(data);
        /**
         * {
            acknowledged: true,
            modifiedCount: 7,
            upsertedId: null,
            upsertedCount: 0,
            matchedCount: 7
            }
         */
    })
});
    